home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_01 / addsub.c < prev    next >
C/C++ Source or Header  |  1992-04-13  |  5KB  |  156 lines

  1.  
  2.  
  3.        /***********************************************
  4.        *
  5.        *       file d:\cips\addsub.c
  6.        *
  7.        *       Functions: This file contains
  8.        *          add_image_array
  9.        *          subtract_image_array
  10.        *
  11.        *       Purpose:
  12.        *          These functions implement
  13.        *          image addition and subtraction.
  14.        *
  15.        *       External Calls:
  16.        *          wtiff.c - does_not_exist
  17.        *                    round_off_image_size
  18.        *                    create_allocate_tiff_file
  19.        *                    write_array_into_tiff_image
  20.        *          tiff.c - read_tiff_header
  21.        *          rtiff.c - read_tiff_image
  22.        *
  23.        *
  24.        *       Modifications:
  25.        *          1 April 1992 - created
  26.        *
  27.        *************************************************/
  28.  
  29. #include "d:\cips\cips.h"
  30.  
  31.      /*******************************************
  32.      *
  33.      *   add_image_array(...
  34.      *
  35.      *   This function adds two ROWSxCOLS image
  36.      *   sections.  The image file named out_name
  37.      *   will receive the sum of the image file
  38.      *   named in1_name and the image file
  39.      *   named in2_name.
  40.      *
  41.      *******************************************/
  42.  
  43.  
  44. add_image_array(in1_name, in2_name, out_name, the_image, out_image,
  45.           il1, ie1, ll1, le1,
  46.           il2, ie2, ll2, le2,
  47.           il3, ie3, ll3, le3)
  48.    char   in1_name[], in2_name[], out_name[];
  49.    int    il1, ie1, ll1, le1,
  50.           il2, ie2, ll2, le2,
  51.           il3, ie3, ll3, le3;
  52.    short  the_image[ROWS][COLS],
  53.           out_image[ROWS][COLS];
  54.  
  55. {
  56.    int    i, j, length, max, width;
  57.    struct tiff_header_struct image_header;
  58.  
  59.  
  60.    if(does_not_exist(out_name)){
  61.       printf("\n\n output file does not exist %s", out_name);
  62.       read_tiff_header(in1_name, &image_header);
  63.       round_off_image_size(&image_header,
  64.                            &length, &width);
  65.       image_header.image_length = length*ROWS;
  66.       image_header.image_width  = width*COLS;
  67.       create_allocate_tiff_file(out_name, &image_header,
  68.                                 out_image);
  69.    }  /* ends if does_not_exist */
  70.  
  71.    read_tiff_header(in1_name, &image_header);
  72.  
  73.    max = 255;
  74.    if(image_header.bits_per_pixel == 4)
  75.       max = 16;
  76.  
  77.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  78.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  79.  
  80.    for(i=0; i<ROWS; i++){
  81.       for(j=0; j<COLS; j++){
  82.          out_image[i][j] = the_image[i][j] + out_image[i][j];
  83.          if(out_image[i][j] > max)
  84.             out_image[i][j] = max;
  85.       }  /* ends loop over j */
  86.    }  /* ends loop over i */
  87.  
  88.    write_array_into_tiff_image(out_name, out_image,
  89.                                il3, ie3, ll3, le3);
  90.  
  91. }  /* ends add_image_array */
  92.  
  93.  
  94.  
  95.  
  96.  
  97.      /*******************************************
  98.      *
  99.      *   subtract_image_array(...
  100.      *
  101.      *   This function subtracts two ROWSxCOLS image
  102.      *   sections.  The image file named out_name
  103.      *   will receive the difference of the image file
  104.      *   named in1_name and the image file
  105.      *   named in2_name.
  106.      *
  107.      *   out_name = in1_name - in2_name
  108.      *
  109.      *******************************************/
  110.  
  111.  
  112. subtract_image_array(in1_name, in2_name, out_name, the_image, out_image,
  113.           il1, ie1, ll1, le1,
  114.           il2, ie2, ll2, le2,
  115.           il3, ie3, ll3, le3)
  116.    char   in1_name[], in2_name[], out_name[];
  117.    int    il1, ie1, ll1, le1,
  118.           il2, ie2, ll2, le2,
  119.           il3, ie3, ll3, le3;
  120.    short  the_image[ROWS][COLS],
  121.           out_image[ROWS][COLS];
  122.  
  123. {
  124.    int    i, j, length, width;
  125.    struct tiff_header_struct image_header;
  126.  
  127.  
  128.    if(does_not_exist(out_name)){
  129.       printf("\n\n output file does not exist %s", out_name);
  130.       read_tiff_header(in1_name, &image_header);
  131.       round_off_image_size(&image_header,
  132.                            &length, &width);
  133.       image_header.image_length = length*ROWS;
  134.       image_header.image_width  = width*COLS;
  135.       create_allocate_tiff_file(out_name, &image_header,
  136.                                 out_image);
  137.    }  /* ends if does_not_exist */
  138.  
  139.    read_tiff_header(in1_name, &image_header);
  140.  
  141.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  142.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  143.  
  144.    for(i=0; i<ROWS; i++){
  145.       for(j=0; j<COLS; j++){
  146.          out_image[i][j] = the_image[i][j] - out_image[i][j];
  147.          if(out_image[i][j] < 0)
  148.             out_image[i][j] = 0;
  149.       }  /* ends loop over j */
  150.    }  /* ends loop over i */
  151.  
  152.    write_array_into_tiff_image(out_name, out_image,
  153.                                il3, ie3, ll3, le3);
  154.  
  155. }  /* ends subtract_image_array */
  156.